<?php

require_once('./discuz_publish.php');
set_time_limit(0);

function get_url($url)
{
	//$url = "http://gaokao.exam8.com/zuowen/manfen/";
	$content = file_get_contents($url);
	preg_match_all('/<div class="lbneirong"><span>.*?<\/div>/', $content, $matches);
	$matches = $matches[0];
	if(is_array($matches))
	{
		foreach($matches as $v)
		{
			$v = substr($v, stripos($v, 'href="')+strlen('href="'));
			$href = substr($v, 0, stripos($v, '"'));

			$v = substr($v, stripos($v, 'title="')+strlen('title="'));
			$title = substr($v, 0, stripos($v, '"'));
			$result[] = array('href'=>$href, 'title'=>$title);
		}
	}
	return $result;
	//file_put_contents( dirname(__FILE__ ) . '/log', var_export($result, true));
}

//$urls = array( 'http://gaokao.exam8.com/zuowen/manfen/', 'http://gaokao.exam8.com/zuowen/manfen/List_1.html');
$urls = array('http://www.exam8.com/computer/djks/dj2/C/ziliao/');
$urls = array('http://www.exam8.com/computer/djks/dj2/C/ziliao/List_32.html', 'http://www.exam8.com/computer/djks/dj2/C/ziliao/List_31.html');

$urls = array(); 

for($i=28; $i>0; $i--)
{
	$urls[] = 'http://www.exam8.com/computer/djks/dj2/C/ziliao/List_' . $i . 'html';
}

//$urls = array('http://www.exam8.com/computer/djks/dj2/C/ziliao/List_28.html', 'http://www.exam8.com/computer/djks/dj2/C/ziliao/List_29.html');
foreach($urls as $url)
{
	
	$result = get_url($url);
	foreach($result as $v)
	{
		get_content($v['href'], $v['title']);
	}
}

exit(0);

function get_content($href, $title)
{
	$base_url = $href;
	$content = file_get_contents($base_url);
	file_put_contents( dirname(__FILE__ ) . '/log', $content . '{br}');
	$i = 0;

	$content = file_get_contents($base_url);

	$content = substr($content, stripos($content, '</STRONG></P>') + strlen('</STRONG></P>'));
	if(stripos($content, '</STRONG></P>') == false)
	{
		$content = substr($content, stripos($content, '<div class="exrdwubjre">') + strlen('<div class="exrdwubjre">'));
	}
	$cont = substr($content, 0, stripos($content, "<div class='showpage'>"));
		//$content = substr($content, stripos($content, 'sans-serif;">') + strlen('sans-serif;">'));
		
	if(empty($title)) break;

	$title = iconv('gbk', 'utf-8', $title);
	$cont = iconv('gbk', 'utf-8', $cont);
////var_dump(escape_s($title), escape_s($cont));

	file_put_contents( dirname(__FILE__ ) . '/log', $title . '{br}'. $cont . '{br}');

	discuz_publish(escape_s($title), escape_s($cont), 77);
//EXIT(0);
	//file_put_contents( dirname(__FILE__ ) . '/log', escape_s($title) . '{br}'. escape_s($cont) . '{br}', FILE_APPEND);
	//exit(0);
}

function escape_s($content)
{
	$content = str_replace( "'", "\'", $content);
	$content = str_replace('"', '\"', $content);
	$content = str_replace('<br />', '', $content);
	$content = preg_replace('/<div.*?>/i', '', $content);
	$content = preg_replace('/<\/div>/i', '', $content);
	$content = preg_replace('/<p>/i', "\r\n", $content);
	return $content;
}
//var_dump($next);




?>